home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / WASTE 1.2 Distribution / WASTE 1.2 / WEArrays.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-18  |  1.2 KB  |  51 lines  |  [TEXT/CWIE]

  1. /*
  2.  *    WEArrays.c
  3.  *
  4.  *    WASTE PROJECT
  5.  *  Utilities for handling handle-based dynamic arrays
  6.  *
  7.  *  Copyright (c) 1993-1996 Marco Piovanelli
  8.  *    All Rights Reserved
  9.  *
  10.  *  C port by Dan Crevier
  11.  *
  12.  */
  13.  
  14.  
  15. #include "WASTEIntf.h"
  16.  
  17. pascal OSErr _WEInsertBlock(Handle h, const void *blockPtr, Size blockSize, SInt32 offset)
  18. {
  19.     Size oldSize;
  20.     OSErr err;
  21.  
  22.     // get handle size
  23.     oldSize = InlineGetHandleSize(h);
  24.     WEASSERT((offset >= 0) && (offset <= oldSize), "\p_WEInsertBlock: bad offset");
  25.  
  26.     // make room for the block to be inserted
  27.     SetHandleSize(h, oldSize + blockSize);
  28.     if ((err = MemError()) != noErr)
  29.         return err;
  30.     BlockMoveData( *h + offset, *h + offset + blockSize, oldSize - offset );
  31.  
  32.     // insert block
  33.     BlockMoveData( blockPtr, *h + offset, blockSize);
  34.  
  35.     return noErr;
  36. }
  37.  
  38. pascal void _WERemoveBlock(Handle h, Size blockSize, SInt32 offset)
  39. {
  40.     Size newSize;
  41.  
  42.     // get handle size minus a "slot"
  43.     newSize = InlineGetHandleSize(h) - blockSize;
  44.     WEASSERT((offset >= 0) && (offset <= newSize), "\p_WERemoveBlock: bad offset");
  45.  
  46.     // compact the handle (this should never fail)
  47.     BlockMoveData( *h + offset + blockSize, *h + offset, newSize - offset );
  48.     SetHandleSize(h, newSize);
  49.     WEASSERT(MemError() == noErr, "\p_WERemoveSlot: failed to shorten handle");
  50. }
  51.